import store from '../store';
import localStorage from 'localStorage';
import { LOCAL_USER } from '../store/user';
// 通用过滤器
exports.csConstFilter = (data, constTYpe) => {
    let tempObj = store.state.csConst[constTYpe].filter((item) => {
        return data === item.id;
    });
    if (tempObj.length === 0) {
        return '';
    } else {
        return tempObj[0].name;
    }
};
// 菜单数字圆点显示
exports.badgeNumFilter = (data) => {
    let num = null;
    if (data > 99) {
        num = 99;
    } else {
        num = data;
    }
    return num;
};
// 根据id取name
exports.getNameById = (id, type, list) => {
    let dictionary = JSON.parse(localStorage.getItem(LOCAL_USER)).dictionaryMap || [];
    let dictionaryArr = type ? dictionary[type].filter(x => x.id === String(id)) : list.filter(x => x.id === String(id));
    if (!dictionaryArr.length) {
        return '';
    } else {
        return dictionaryArr[0].name;
    }
};
// 根据type取name，显示上传文件类型 by jh
exports.getNameByType = (type) => {
    let dictionary = JSON.parse(localStorage.getItem(LOCAL_USER)).fileFloderDicVoList || [];
    let dictionaryArr = dictionary.filter(x => x.type === String(type));
    if (!dictionaryArr.length) {
        return '';
    } else {
        return dictionaryArr[0].name;
    }
};
// 根据部门 id 取部门 name 或者根据用户id 取 用户name  type:departmentList(部门)  userEntityList(用户)
exports.getDepartmentById = (id, type) => {
    let dictionary = JSON.parse(localStorage.getItem(LOCAL_USER)).department || [];
    let dictionaryArr = []
    // 超级管理员的id是1
    if ( type === 'userEntityList' && id.toString() === '1') {
        return '超级管理员'
    }
    if (type === 'userEntityList') {
        dictionaryArr = dictionary[type].filter(x => x.id === String(id));
    } else if (type === 'departmentList') {
        dictionaryArr = dictionary[type].filter(x => x.deptId === String(id));
    }
    if (!dictionaryArr.length) {
        return '';
    } else if (type === 'userEntityList') {
        return dictionaryArr[0].userName;
    } else if (type === 'departmentList') {
        return dictionaryArr[0].deptName;
    }
};
//保留两位小数 && 整数部分每三位加逗号  by jh
exports.format1 = function (num) {
    num = Number(num);
    if (!num ) {
        return '0.00';
    }
    return ((num).toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
}
// % 保留两位小数 && 整数部分每三位加逗号  by yyf
exports.format2 = function (num) {
    if ( num.indexOf('%') ) {
        num = Number( num.substring(0, num.length - 1) ) * 100;
    } else {
        num = Number(num) * 100
    }
    if (!num) {
        return '0.00';
    }
    return ((num).toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')
}
// 省市区 通过 id 获取 name  by yyf
exports.getRegionName = function (str) {
    let regionData = JSON.parse(localStorage.region);  // 所有省市区数据
    let province = ''; // 省
    let city = ''; // 市
    let district = ''; // 区
    let regionNameStr = '';
    if (str) {
        let regionIdArr = str.split(',');
        // 省
        regionData.map((item)=>{
            if (Number(regionIdArr[0]) === item.id) {
                province = item.district;
            }
            if (Number(regionIdArr[1]) === item.id) {
                city = item.district;
            }
            if (Number(regionIdArr[2]) === item.id) {
                district = item.district;
            }
        })
        regionNameStr = province + city + district;
    }
    return regionNameStr;
}
//用户过滤 客户回访记录 by JH
exports.getUserName = function (id,arr) {
    let data = [...arr]
    let userName = ''
    data.map((item)=>{
        if (id === item.id ) {
            userName = item.cnName
        }
    })
    return userName
}

